iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 28
0
自我挑戰組

自我挑戰 Ruby 刷題 30 天系列 第 28

Day28 - Codewars 刷題

  • 分享至 

  • xImage
  •  

先補上昨天未解完 Codewars LV4 的答案,
時間複雜度沒有解決,若有更好解法可在下方留言讓我知道喔~~

到 LV4 解起來花的時間比平常更久,
過程中會一直找方法,和專案蠻像的,
遇到問題,想辦法解決它。


題目(Next bigger number with the same digits)

You have to create a function that takes a positive integer number and returns the next bigger number formed by the same digits:

12 ==> 21
513 ==> 531
2017 ==> 2071
If no bigger number can be composed using those digits, return -1:

9 ==> -1
111 ==> -1
531 ==> -1
def next_bigger(n)
  #your code here
end

Test.assert_equals(next_bigger(12),21)
Test.assert_equals(next_bigger(513),531)
Test.assert_equals(next_bigger(2017),2071)
Test.assert_equals(next_bigger(414),441)
Test.assert_equals(next_bigger(144),414)

題目(Most frequently used words in a text)

Write a function that, given a string of text (possibly with punctuation and line-breaks), returns an array of the top-3 most occurring words, in descending order of the number of occurrences.

Assumptions:
A word is a string of letters (A to Z) optionally containing one or more apostrophes (') in ASCII. (No need to handle fancy punctuation.)
Matches should be case-insensitive, and the words in the result should be lowercased.
Ties may be broken arbitrarily.
If a text contains fewer than three unique words, then either the top-2 or top-1 words should be returned, or an empty array if a text contains no words.
Examples:
top_3_words("In a village of La Mancha, the name of which I have no desire to call to
mind, there lived not long since one of those gentlemen that keep a lance
in the lance-rack, an old buckler, a lean hack, and a greyhound for
coursing. An olla of rather more beef than mutton, a salad on most
nights, scraps on Saturdays, lentils on Fridays, and a pigeon or so extra
on Sundays, made away with three-quarters of his income.")
# => ["a", "of", "on"]

top_3_words("e e e e DDD ddd DdD: ddd ddd aa aA Aa, bb cc cC e e e")
# => ["e", "ddd", "aa"]

top_3_words("  //wont won't won't")
# => ["won't", "wont"]
Bonus points (not really, but just for fun):
Avoid creating an array whose memory footprint is roughly as big as the input text.
Avoid sorting the entire array of unique words.
def top_3_words(text)
  # ...
end

Test.assert_equals(top_3_words("a a a  b  c c  d d d d  e e e e e"), ["e", "d", "a"])
Test.assert_equals(top_3_words("e e e e DDD ddd DdD: ddd ddd aa aA Aa, bb cc cC e e e"), ["e", "ddd", "aa"])
Test.assert_equals(top_3_words("  //wont won't won't "), ["won't", "wont"])
Test.assert_equals(top_3_words("  , e   .. "), ["e"])
Test.assert_equals(top_3_words("  ...  "), [])
Test.assert_equals(top_3_words("  '  "), [])
Test.assert_equals(top_3_words("  '''  "), [])
Test.assert_equals(top_3_words("""In a village of La Mancha, the name of which I have no desire to call to
mind, there lived not long since one of those gentlemen that keep a lance
in the lance-rack, an old buckler, a lean hack, and a greyhound for
coursing. An olla of rather more beef than mutton, a salad on most
nights, scraps on Saturdays, lentils on Fridays, and a pigeon or so extra
on Sundays, made away with three-quarters of his income."""), ["a", "of", "on"])


影片解題:
Yes


答案:

# Next bigger number with the same digits
def next_bigger(n)
  n.to_s.split('').permutation(n.to_s.length).map(&:join).select{ |x| return x.to_i if x.to_i > n }
  -1
end
next_bigger(1234567980)
#會有時間複雜度喔



# Most frequently used words in a text
def top_3_words(text)
  text.scan(/[A-Za-z']+/)
      .select{ |x| /[A-Za-z]/ =~ x }
      .group_by{ |x| x.downcase }
      .sort_by{ |k, v| -v.count }
      .first(3)
      .map(&:first)
end

本文同步發布於 小菜的 Blog https://riverye.com/


上一篇
Day27 - Codewars 刷題
下一篇
Day29 - Codewars 刷題
系列文
自我挑戰 Ruby 刷題 30 天31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言